var app = getApp();
var commonURLYY = app.data.newURL;
var commonURLXX = app.data.lastURL;
var GZHid=53;//公众号id
var openid = app.data.openid;
var page = 1;
var pagesize = 12;
var dataLists={};//缓存商品列表数据
Page({
	data: {
		goodsList: [
			// {image:'',description: '【YBUGQI官方正品】2017新款羊皮质感双肩女背包【买一得三】',price: '23.9',stock:'7.2'},
			// {image:'',description: '【YBUGQI官方正品】2017新款羊皮质感双肩女背包【买一得三】',price: '23.9',stock:'7.2'},
			// {image:'',description: '【YBUGQI官方正品】2017新款羊皮质感双肩女背包【买一得三】',price: '23.9',stock:'7.2'},
			// {image:'',description: '【YBUGQI官方正品】2017新款羊皮质感双肩女背包【买一得三】',price: '23.9',stock:'7.2'},
			// {image:'',description: '【YBUGQI官方正品】2017新款羊皮质感双肩女背包【买一得三】',price: '23.9',stock:'7.2'},
			// {image:'',description: '【YBUGQI官方正品】2017新款羊皮质感双肩女背包【买一得三】',price: '23.9',stock:'7.2'},
			// {image:'',description: '【YBUGQI官方正品】2017新款羊皮质感双肩女背包【买一得三】',price: '23.9',stock:'7.2'},
			// {image:'',description: '【YBUGQI官方正品】2017新款羊皮质感双肩女背包【买一得三】',price: '23.9',stock:'7.2'},
			// {image:'',description: '【YBUGQI官方正品】2017新款羊皮质感双肩女背包【买一得三】',price: '23.9',stock:'7.2'}
		],
		value: '',//input输入内容
		showsearch_list: false,
		scrollTop: 0
	},
	onLoad: function (option) {
		page = 1;
		var that = this;
		console.log(option.value);
		this.setData({
			value: option.value
		})
		wx.request({
			url: commonURLYY+'op=search_goods_name',
			data: {
				key: option.value,
				page: page,
				pagesize: pagesize
			},
			header: {
				'content-type': 'application/json'
			},
			success: function (res) {
				that.setData({
					goodsList: res.data.data
				})
			}
		})
	},
	onShow: function () {
		page = 1;
		wx.request({
	      url:commonURLXX+ 'op=uniacid_shop',
	      header: {
	        'content-type': 'application/json'
	      },
	      success:function (res) {
	         wx.setNavigationBarTitle({title:res.data.sname});//设置title文字
	      }
	    })
	},
	goTop: function(e){
	    this.setData({
	        scrollTop:0
	    })
	},
	scrollTopFun:function(e){
	 // 容器滚动时将此时的滚动距离赋值给 this.data.scrollTop
	 if(e.detail.scrollTop > 500){
	     this.setData({
	        floorstatus: true
	     });
	 }else {
	     this.setData({
	        floorstatus: false
	     });
	    }
    },
	onBottom: function () {
		var that = this;
		page++;
		wx.request({
			url: commonURLYY + 'op=search_goods_name',
			data: {
				key: this.data.value,
				page: page,
				pagesize: pagesize
			},
			header: {
				'content-type':'application/json'
			},
			success: function (res) {
				if(res.data.data.length < 1) {
					wx.showToast({
						title: '全部商品加载完成',
						icon: 'success',
						duration: 1000
					})
					return false;
				}
				wx.showToast({
					title: '商品加载中...',
					icon: 'loading',
					duration: 700
				})
				var i = 0,lists = [],length = res.data.data.length;
				for(;i < length; i++){
					var obj = {};
					var indexid = obj["id"] = res.data.data[i].id;
					obj['gimg'] = res.data.data[i].gimg;
					obj['gname'] = res.data.data[i].gname;
					obj["oprice"]=res.data.data[i].oprice;//单买价
					obj["gprice"]=res.data.data[i].gprice;//团购价
					obj["peoplenum"]=res.data.data[i].groupnum;
					obj["unit"]=res.data.data[i].unit;
		            obj["mprice"]=res.data.data[i].mprice;//市场价
		            obj["salenum"]=res.data.data[i].salenum;
		            obj["xiangqing"]=res.data.data[i].gdesc;
		            obj["shuxing"]=res.data.data[i].params;
		            obj["advs"]=res.data.data[i].advs;
		            obj["kucun"]=res.data.data[i].gnum;
		            obj["selltype"]=res.data.data[i].selltype;
		            obj["hasoption"]=res.data.data[i].hasoption;//是否有规格 0没有 ，1有
		            obj["weight"]=res.data.data[i].weight;//重量，用于计算运费
		            obj["deliverytype"]=res.data.data[i].deliverytype;//配送方式
					if(res.data.data[i].gnum=="0"){
		              obj["smtuan"]="售罄";
		              obj["backg"]="#aaa";
		              obj["url"]='';
		            }else{
		              obj["smtuan"]="随意团  >";
		              obj["url"]='../index_detail/index_detail?id='+indexid;
		            }
		            lists.push(obj);
            		dataLists[indexid]=obj;
				}
				that.setData({
		            goodsList:that.data.goodsList.concat(lists)
		        });
			}
		})
	},
	oninput_change: function (event){ //显示搜索字样
		this.setData({
			value: event.detail.value,
			search_text: true,
			showsearch_list: true
		})
		if(event.detail.value == ''){
			this.setData({
				search_text: false
			})
		}
	},
	onclear: function () {
		this.setData({
			value: '',
			search_text: false
		})
	},
	onclose_search: function () {
		wx.navigateBack({
		  delta: 1
		})
	},
	go_detail: function (e) {    //去商品详情页
		var _id = e.currentTarget.dataset.id;
		var _selltype = e.currentTarget.dataset.selltype;
		if(_selltype == '0'){
			wx.navigateTo({
				url: '../../index_only_detail/index_only_detail?id='+_id
			})
		}else{
			wx.navigateTo({
				url: '../../index_detail/index_detail?id='+_id
			})
		}
	}
})